/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config;

import opiam.admin.faare.config.javabeans.JBTrombiParams;

import org.apache.commons.digester.Digester;

import org.apache.log4j.Logger;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.io.InputStream;


/**
 * Trombinoscope parameters configuration loading.
 *
 * The XML file must contain the following line :
 * <!DOCTYPE mapping PUBLIC
 *         "-//OPIAM ADMIN//DTD Trombinoscope Service Configuration 1.0//EN"
 *         "http://www.opiam.org/admin/dtd/trombinoscope_conf.dtd">
 *
 * The second line is registered to the digester (it does not need to be an actual URL).
 *
 *
 */
public final class TrombinoscopeConfig implements ErrorHandler
{
    /** Log4J instance. */
    private static Logger _logger = Logger.getLogger(TrombinoscopeConfig.class.getName());

    /** DOCTYPE key. */
    public static final String TROMBI_CONFIG_DTD_KEY =
                                          "-//OPIAM ADMIN//DTD Trombinoscope Service Configuration 1.0//EN";

    /** DOCTYPE URL. */
    public static final String TROMBI_CONFIG_DTD_URL = "/config/service/trombinoscope/trombinoscope_conf.dtd";

    /** parser. */
    private static Digester _digester = null;

    /** current instance. */
    private static TrombinoscopeConfig _instance = null;

    /** parser exception. */
    private static SAXParseException _saxParseException = null;

    /**
     * Constructor for TrombinoscopeConfig.
     */
    private TrombinoscopeConfig()
    {
        super();
    }

    /**
     * Parser initialization with DTD fields.
     */
    private static void initProfileDigester()
    {
        _saxParseException = null;
        _digester = new Digester();

        _digester.setErrorHandler(_instance);
        _digester.setValidating(true);

        _digester.register(TROMBI_CONFIG_DTD_KEY,
            TrombinoscopeConfig.class.getResource(TROMBI_CONFIG_DTD_URL)
                                     .toString());

        _digester.addObjectCreate("trombiparams",
            "opiam.admin.faare.config.javabeans.JBTrombiParams");
        _digester.addSetProperties("trombiparams");

        _digester.addObjectCreate("trombiparams/photoColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetNext("trombiparams/photoColor", "setPhotoColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetProperties("trombiparams/photoColor");

        _digester.addObjectCreate("trombiparams/textColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetNext("trombiparams/textColor", "setTextColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetProperties("trombiparams/textColor");
    }

    /**
     * Gets TrombinoscopeConfig instance.
     *
     * @return TrombinoscopeConfig instance.
     */
    public static TrombinoscopeConfig getInstance()
    {
        if (_instance == null)
        {
            _instance = new TrombinoscopeConfig();
            initProfileDigester();
        }

        return _instance;
    }

    /**
     * Gets Trombinoscope parameters.
     *
     * @param in configuration stream
     *
     * @return Trombinoscope parameters
     *
     * @throws IOException exception while reading stream.
     * @throws SAXException exception while parsing data.
     */
    public JBTrombiParams readConfig(InputStream in)
        throws IOException, SAXException
    {
        JBTrombiParams result = null;
        result = (JBTrombiParams) _digester.parse(in);

        if (_saxParseException != null)
        {
            _logger.debug("Exception : " + _saxParseException.getMessage());
            throw new SAXException(_saxParseException.getMessage());
        }

        return result;
    }

    /**
     * parsing error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void error(SAXParseException saxParseException)
    {
        _logger.debug(" error exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * parsing fatal error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void fatalError(SAXParseException saxParseException)
    {
        _logger.debug("fatalError exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * parsing warning callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void warning(SAXParseException saxParseException)
    {
        _logger.debug("warning : " + saxParseException.getMessage());
    }
}
